package com.apts.servise;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.apts.jfinalbase.BaseJsonPojo;
import com.apts.jfinalbase.BaseServise;
import com.apts.model.TBpe;
import com.apts.model.TbProductType;
import com.apts.pojo.NomalPojo;

public class ProductServise extends BaseServise {

	public static final ProductServise Service = new ProductServise();

	// 查找所有品种信息
	public BaseJsonPojo Findall() {
		List<TbProductType> list = TbProductType.dao.findAll();
		List<Map<String, Object>> productSet = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> map = new HashMap<String, Object>(0);
			TbProductType producttype = list.get(i);
			map.put("proID", producttype.get("typecode"));
			map.put("proName", producttype.get("typename"));
			map.put("proImg", producttype.get("profileimg"));
			map.put("proInfo", producttype.get("typeinfo"));
			productSet.add(map);
		}
		Map<String, Object> result = new HashMap<String, Object>(0);
		result.put("count", productSet.size());
		result.put("list", productSet);
		return new NomalPojo(result);
	}

	// 根据id精确查找产品类型信息
	public BaseJsonPojo Findbyid(String typecode) {
		TbProductType producttype = TbProductType.dao.findById(typecode);
		Map<String, Object> map = new HashMap<String, Object>(0);
		map.put("proID", producttype.get("typecode"));
		map.put("proName", producttype.get("typename"));
		map.put("proImg", producttype.get("profileimg"));
		map.put("proInfo", producttype.get("typeinfo"));
		return new NomalPojo(map);
	}

	// 根据品种名模糊查找品种信息
	public BaseJsonPojo Findbytypename(String typename) {
		List<TbProductType> list = TbProductType.dao.findbytypename(typename);
		List<Map<String, Object>> productSet = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> map = new HashMap<String, Object>(0);
			TbProductType producttype = list.get(i);
			map.put("proID", producttype.get("typecode"));
			map.put("proName", producttype.get("typename"));
			map.put("proImg", producttype.get("profileimg"));
			map.put("proInfo", producttype.get("typeinfo"));
			productSet.add(map);
		}
		Map<String, Object> result = new HashMap<String, Object>(0);
		result.put("count", productSet.size());
		result.put("list", productSet);
		return new NomalPojo(result);
	}

	// 根据企业名查找品种信息
	public BaseJsonPojo Findbyenterprise(String epname1, int start, int end) {
		List<TbProductType> list = TbProductType.dao.findbyenterprise(epname1, start, end);

		Map<String, Object> map = new HashMap<String, Object>(0);
		for (int i = 0; i < list.size(); i++) {
			TbProductType producttype = list.get(i);
			map.put("typecode" + i, producttype.get("typecode"));
			map.put("typename" + i, producttype.get("typename"));
			map.put("profileimg" + i, producttype.get("profileimg"));
			map.put("typeinfo" + i, producttype.get("typeinfo"));
			List<TBpe> tbpe = TBpe.dao.PEFindenterprise((String) producttype.get("typecode"));
			String epname = "";
			for (int j = 0; j < tbpe.size(); j++) {
				TBpe tbpe1 = tbpe.get(j);
				epname = epname + "," + tbpe1.getStr("epname");
			}
			map.put("epname" + i, epname);//
		}
		return new NomalPojo(map);
	}

	public void Add(Map<String, Object> map) {
		TbProductType.dao.insert(map);
	}

	public void Update(Map<String, Object> map) {
		TbProductType.dao.update(map);
	}

	public void Delete(String typecode) {
		TbProductType.dao.Delete(typecode);
	}

	//////////////////////////////////////
	// 添加企业到品种
	public void Addenterprise(String typecode, String regcode) {
		TBpe.dao.PEadd(typecode, regcode);

	}

	// 添加企业到品种
	public void deleteenterprise(String typecode, String regcode) {
		TBpe.dao.PEdelete(typecode, regcode);
	}

}
